exports.MMStoreModel = Core.Class.subclass(
{
	classname:'MMStoreModel',
								   
    initialize: function(storeJson) 
    {
    	this._storeJson = null;
    	
    	if(storeJson) {
    		this._storeJson = storeJson;
    	} else {
    		this._storeJson = {"storeId": -1,"mallId": 1,"itemId": "1_1_0","positionX": 0,"positionY": 0,"constructionStartTime": 0,"lastCollected": -1, "numberOfCollections": 0, "restockingStartTime": 0, "upgradeState" : 0, "upgradeStartTime" : 0 };
    	}
    	
    	if(+this._storeJson.lastCollected == -1)
    	{
    		this._storeJson.lastCollected = MMGameState.getServerTime();
    	}
    },
    
    canUpgradeBePurchased: function(currency, cost)
    {
		return MMGameState.getCurrentUser().isValidPurchase(currency, cost);
    },
    
    canRestockBePurchased: function(currency, cost)
    {
		return MMGameState.getCurrentUser().isValidPurchase(currency, cost);
    },
    
    canUpgradeStore: function()
    {
    	var currentUpgradeLevel = this.getUpgradeState();
    	var targetLevel = +currentUpgradeLevel + 1;
    	
    	if(this.getItemModel().custom_attributes && this.getItemModel().custom_attributes.upgrade_attributes[targetLevel])
    	{
    		return true;
    	}
    	return false;
    	
    },
    
    canRestockStore: function()
    {
    	var currentNoOfCollections = this.getNumberOfCollections();
    	var maxNoOfCollections = this.getTotalNoOfCollections();
    	//if(currentNoOfCollections >= maxNoOfCollections)
    	if((maxNoOfCollections - currentNoOfCollections) < maxNoOfCollections)
    	{
    		return true;
    	}
    	return false;
    },
    
    getTotalNoOfCollections: function()
    {
    	var totalCollections = this.getItemModel().custom_attributes.no_of_collections;
    	return totalCollections;
    },
    
    upgradeStore: function()
    {
    	if(!this.canUpgradeStore())
    	{
    		return;
    	}
		
		var pricing = this.getUpgradePricing();
    	var operation = null;
    	
    	if(pricing.currency == kCurrencyType.kCurrencyTypeNuggets)
    	{
    		operation = kOperationType.kOperationSubtractNuggets;
    	}
    	else
    	{
			operation = kOperationType.kOperationSubtractCoins;
    	} 
    	MMGameState.getCurrentUser().updateCurrency(operation, pricing.cost);
    	
    	
    	//Updating Time
		this.setUpgradeStartTime(MMGameState.getServerTime());
		this.updateModel();
    },
    
   	restockStore: function()
    {
    	if(!this.canRestockStore())
    	{
    		return;
    	}
		
		var pricing = this.getRestockPricing();
    	var operation = null;
    	
//    	if(pricing.currency == kCurrencyType.kCurrencyTypeNuggets)
//    	{
//    		operation = kOperationType.kOperationSubtractNuggets;
//    	}
//    	else
    	{
			operation = kOperationType.kOperationSubtractCoins;
    	} 
    	MMGameState.getCurrentUser().updateCurrency(operation, pricing);
    	
    	//Updating Time
		this.setRestockStartTime(MMGameState.getServerTime());
		this.updateModel();
    },    
    
    getRestockStartTime: function()
    {
    	if(this._storeJson)
		{
			return this._storeJson.restockingStartTime;
		}
		return null;
    },
    
    setRestockStartTime: function(startTime)
    {
    	if(this._storeJson)
		{
			this._storeJson.restockingStartTime = startTime;
		}
    },
    
    getRestockPricing: function()
    {
    	var restockPrice = this.getItemModel().custom_attributes.restocking_cost;
    	return restockPrice;
    },
    
    upgradeRemainingTime : function()
	{
		var currentTime = MMGameState.getServerTime();
		var upgradeStartTime = this.getUpgradeStartTime();
		var totalUpgradeTime = this.getTotalUpgradeTime();
		
		var diff = totalUpgradeTime - (currentTime - upgradeStartTime);
		
		if(diff < 0)
		{
			diff = 0;
		}
		
		return diff;
	},
	
	restockRemainingTime : function()
	{
		var currentTime = MMGameState.getServerTime();
		var restockStartTime = this.getRestockStartTime();
		var totalRestockTime = this.getTotalRestockTime();
		
		var diff = totalRestockTime - (currentTime - restockStartTime);
		
		if(diff < 0)
		{
			diff = 0;
		}
		
		return diff;
	},
	
	getTotalRestockTime: function()
	{
		var restockTime = this.getItemModel().custom_attributes.restocking_time;
    	return restockTime;
	},
	
	getUpgradeProgressPercentage: function()
	{
		if(this.isUpgradeInProgress())
		{
			var totalUpgradeTime = this.getTotalUpgradeTime();
			var upgradeRemainingTime = this.upgradeRemainingTime();
			var upgradeTimePassed = totalUpgradeTime - upgradeRemainingTime;
			var upgradePercentage = (upgradeTimePassed/totalUpgradeTime)*100;
			return upgradePercentage;
		}	
		return null;
	},
	
	getRestockProgressPercentage: function()
	{
		if(this.isRestockInProgress())
		{
			var totalRestockTime = this.getTotalRestockTime();
			var restockRemainingTime = this.restockRemainingTime();
			var restockTimePassed = totalRestockTime - restockRemainingTime;
			var restockPercentage = (restockTimePassed/totalRestockTime)*100;
			return restockPercentage;
		}	
		return null;
	},
	
	getRemainingStockInPercentage: function()
	{
		var totalCollections = this.getTotalNoOfCollections();
		var currentCollections = this.getNumberOfCollections();
		
		var percentageRemaining = Math.ceil((1 - (currentCollections/totalCollections))*100);
		return percentageRemaining;
	},
	
	getIsStockEmpty: function()
	{
		var currentStock = this.getNumberOfCollections();
		var totalStock = this.getTotalNoOfCollections();
		if(currentStock >= totalStock)
		{
			return true;
		}
		return false;
	},
	
	getTotalUpgradeTime: function()
	{
		var currentUpgradeLevel = this.getUpgradeState();
    	var targetLevel = +currentUpgradeLevel + 1;
    	
    	if(this.getItemModel().custom_attributes && this.getItemModel().custom_attributes.upgrade_attributes[targetLevel])
    	{
    		var upgradeAtt = this.getItemModel().custom_attributes.upgrade_attributes[targetLevel];
    		return upgradeAtt.time;
    	}
	},
	
	getUpgradePricing: function()
	{
		var currentUpgradeLevel = this.getUpgradeState();
    	var targetLevel = +currentUpgradeLevel + 1;
    	
    	if(this.getItemModel().custom_attributes && this.getItemModel().custom_attributes.upgrade_attributes[targetLevel])
    	{
    		var pricing = new Object();
    		var upgradeAtt = this.getItemModel().custom_attributes.upgrade_attributes[targetLevel];
    		pricing.cost = upgradeAtt.cost;
    		pricing.currency = upgradeAtt.currency;
    		return pricing;
    	}		
    	return null;
	},
    
    restockCompleted: function()
    {
    	this.setNumberOfCollections(0);
    	this.setRestockStartTime(0);
    	
    	this.updateModel();
    },
    
    
    upgradeCompleted: function()
    {
    	var currentUpgradeLevel = this.getUpgradeState();
    	var targetLevel = +currentUpgradeLevel + 1;
    	
    	this.setUpgradeState(targetLevel);
    	this.setUpgradeStartTime(0);
    	
    	this.updateModel();
    },    
    
    isUpgradeInProgress: function()
    {
    	var upgradeStartTime = this.getUpgradeStartTime();
    	if(upgradeStartTime > 0)
    	{
    		return true;
    	}
    	return false;
    },
    
    isRestockInProgress: function()
    {
    	var restockStartTime = this.getRestockStartTime();
    	if(restockStartTime > 0)
    	{
    		return true;
    	}
    	return false;
    },
    
    updateModel: function()
	{
		MMGameState.getCurrentUser().updateModel();
	},
	
	setStoreId: function(storeId)
	{
		if(storeId && this._storeJson)
		{
			this._storeJson.storeId = storeId;
		}
	},
	
	getLastCollected: function()
	{
		if(this._storeJson)
		{
			var lastCollected = this._storeJson.lastCollected; 
			return lastCollected;
		}
		return null;
	},
	
	setLastCollected: function(time)
	{
		this._storeJson.lastCollected = time; 
	},
	
	getUpgradeState: function()
	{
		if(this._storeJson)
		{
			var upgradeState = this._storeJson.upgradeState; 
			return upgradeState;
		}
		return null;
	},
	
	setUpgradeState: function(upgradeState)
	{
		this._storeJson.upgradeState = upgradeState; 
	},
	
	getUpgradeStartTime: function()
	{
		if(this._storeJson)
		{
			var upgradeStartTime = this._storeJson.upgradeStartTime; 
			return upgradeStartTime;
		}
		return null;
	},
	
	setUpgradeStartTime: function(upgradeStartTime)
	{
		this._storeJson.upgradeStartTime = upgradeStartTime; 
	},
	
	getConstructionStartTime: function()
	{
		if(this._storeJson)
		{
			var constructionStartTime = this._storeJson.constructionStartTime; 
			return constructionStartTime;
		}
		return null;
	},		
	
	getNumberOfCollections: function()
	{
		if(this._storeJson)
		{
			var numberOfCollections = this._storeJson.numberOfCollections; 
			return numberOfCollections;
		}
		return null;
	},		
	
	setNumberOfCollections: function(collections)
	{
		this._storeJson.numberOfCollections = collections; 
	},		
	
	getPosition: function()
	{
		if(this._storeJson)
		{
			var position = new Core.Point(this._storeJson.positionX, this._storeJson.positionY); 
			return position;
		}
		return null;
	},
	
	setPosition: function(position)
	{
		if(this._storeJson)
		{
			this._storeJson.positionX = position.getX();
			this._storeJson.positionY = position.getY();
		}
	},
	
	getItemModel: function()
	{
		var itemModel = GAGameLoader.getStoreHandler().getStoreObjectForKey(this.getItemId());
		return itemModel;
	},
	
	getName: function()
	{
		return this.getItemModel().name;
	},
	
	getRewardAmount: function()
	{
		

		var model = this.getItemModel();
		var upgradeState = this.getUpgradeState();
		var reward = model.custom_attributes.reward[upgradeState].amount;
		return reward;
//		var priceForBucket = model.buy_prices["default"];            
//        var defaultBuyPrice = TCUtil.getPrice(priceForBucket);
//		return defaultBuyPrice;
	},
	
	getStoreId: function()
	{
		if(this._storeJson)
		{
			return this._storeJson.storeId;
		}
		return null;
	},
	
	setMallId: function(mallId)
	{
		if(mallId && this._storeJson)
		{
			this._storeJson.mallId = mallId;
		}
	},
	
	getMallId: function()
	{
		if(this._storeJson)
		{
			return this._storeJson.mallId;
		}
		return null;
	},
	
	setItemId: function(itemId)
	{
		if(itemId && this._storeJson)
		{
			this._storeJson.itemId = itemId;
		}
	},
	
	getItemId: function()
	{
		if(this._storeJson)
		{
			return this._storeJson.itemId;
		}
		return null;
	},
    						   
	destroy: function()
	{
		this._storeJson = null;	
	},
});